#include <stdio.h>
#include "list.h"

sj_void list_init(struct list *list)
{
    list->prev =list;
    list->next =list;
}

sj_void list_add_tail(struct list *n,struct list *k)
{
	struct list *next = k->next;

    n->prev =k;
    n->next =next;

    next->prev=n;
    k->next=n;
}

sj_void list_add_head(struct list *n,struct list *k)
{
	struct list *prev = k->prev;

    n->prev =prev;
    n->next =k;

    prev->next=n;
    k->prev=n;
}

struct list *list_next(struct list *l)
{	
	return l->next;
}

struct list *list_prev(struct list *l)
{
	return l->prev;
}

sj_void list_iter_init(struct list *k, struct list_iter *iter)
{
	iter->i_list=k;
	iter->i_cur=NULL;
}

struct list *list_iter_next(struct list_iter *iter)
{
	struct list *list = iter->i_list;
	struct list *last = iter->i_cur;

	if(last)
	{
		iter->i_cur = last->next;
	}
	else 
	{
		iter->i_cur=list->next;
	}		

	return iter->i_cur;
}

struct list *list_iter_prev(struct list_iter *iter)
{
	struct list *list = iter->i_list;
	struct list *last = iter->i_cur;
	
	if(last)
	{
		iter->i_cur = last->prev;
	}
	else 
	{
		iter->i_cur=list->prev;
	}		

	return iter->i_cur;
}
sj_void list_iter_exit(struct list_iter *iter)
{
	iter->i_cur=NULL;
}

sj_bool list_iter_is_head(struct list_iter *iter)
{
	if(iter->i_cur)
	{
		if(iter->i_cur == iter->i_list)
		{
			return sj_true;
		}
	}
	return sj_false;
}

